from flask import Blueprint, jsonify, request,redirect
from db import db1
from utils.comm import get_data
import time

bp_users = Blueprint('yh', __name__)


@bp_users.route('/newsM')
def newsM():
    sql = """select * from news"""
    res = db1.find_all(sql)
    return jsonify({"code": 200, "list": res})


@bp_users.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        data = get_data()
        print(data)
        sql = """select * from user where username = '%s'""" % data['username']
        res = db1.find_one(sql)
        if res:
            if res['password'] == data['password']:
                return jsonify({"code": 200, 'userid': res['id']})
            else:
                return jsonify({"code": 10010, 'mes': '密码错误'})
        else:
            return jsonify({"code": 10011, 'mes': '不存在此用户'})


@bp_users.route('/pls', methods=['POST', 'GET'])
def pls():
    if request.method == 'POST':
        data = get_data()
        print(data)
        sql = """insert into pl(content,userid,news_id) values('%s',%d,%d)""" % (
        data['pl'], int(data['userid']), int(data['id']))
        db1.insert(sql)
        return jsonify({"code": 200})


@bp_users.route('/xq')
def xq():
    id = request.args.get('id')
    sql = """select * from news where id = %d""" % int(id)
    res = db1.find_one(sql)
    timess = int(time.time())
    if res['times']:
        nowtime = int(time.time())
        if nowtime - int(res['times']) > 3600:
            sql3 = """update news set total_views = (total_views+1) where id = %d"""%int(id)
            db1.insert(sql3)
            sql4 = """update news set times = %d where id = %d""" %(nowtime,int(id))
            db1.insert(sql4)
        else:
            return jsonify({"code":200,"list":res})
    else:
        sql2 = """update news set times = %d where id = %d""" %(timess,int(id))
        db1.insert(sql2)
        sql4 = """update news set total_views = %d where id = %d""" %(1,int(id))
        db1.insert(sql4)
    return jsonify({"code": 200, "list": res})


#模拟三方登录
@bp_users.route('/sfM', methods=['POST', 'GET'])
def sfM():
    if request.method == 'POST':
        data = get_data()
        token = data['token']
        sql = """select * from sf_login where token = '%s'"""%token
        res = db1.find_one(sql)
        if res:
            return jsonify({'code':200,'type':1})
        else:
            return jsonify({"url":'http://localhost:8080/sf_zhuanje','type':2})



@bp_users.route('/dengluM', methods=['POST', 'GET'])
def dengluM():
    if request.method == 'POST':
        data = get_data()
        token = data['token']
        mobile = data['mobile']
        uid = int(data['uid'])
        sql = """insert into s_user(username) values ('%s')"""%(mobile)
        db1.insert(sql)
        sql1 = """select * from s_user where username= ('%s')"""%(mobile)
        res = db1.find_one(sql1)
        sql2 = """insert into sf_login(name,userid,uid,token) values ('%s',%d,%d,'%s')"""%('微博',int(res['id']),uid,token)
        db1.insert(sql2)
        return jsonify({'code':200,"mes":"登录成功","url":'http://localhost:8080'})



#停车厂
@bp_users.route('/stopM', methods=['POST', 'GET'])
def stopM():
    if request.method == 'POST':
        data = get_data()
        nowtime = int(time.time())
        #查看是否是本小区的
        sql = """select * from stop_user where cph = '%s'"""%(data['cph'])
        res = db1.find_one(sql)
        if res:
            #每小时收费一块五
            sql2 = """insert into d_shop(userid,money,stime) values (%d,%.1f,%d)"""%(int(res['id']),,)
        else:
            #每小时收费3块
            # sql = """insert into d_stop()"""
            pass


























